package com.example.zy5_project_jetpack.mvp.view.Fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.zy5_project_jetpack.Manager.RetrofitManager;
import com.example.zy5_project_jetpack.R;
import com.example.zy5_project_jetpack.mvp.model.ApiService;
import com.example.zy5_project_jetpack.mvp.presenter.PresenterData;
import com.example.zy5_project_jetpack.mvp.view.Adapter.HomeRecyclerAdapter;
import com.example.zy5_project_jetpack.mvp.view.Bean.HomeBean;
import com.example.zy5_project_jetpack.mvp.view.IView;
import com.example.zy5_project_jetpack.mvp.view.SofaFragment.VideoFragment;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class HomeFragment extends Fragment implements IView {

    private PresenterData presenterData;
    private HomeRecyclerAdapter homeRecyclerAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        PresenterData presenterData = new PresenterData(this);
        presenterData.startData();

//        initData();
       /* ApiService apiService = RetrofitManager.getInstance().getApiService(ApiService.class);
        apiService.getHomeInfo().enqueue(new Callback<HomeBean>() {
            @Override
            public void onResponse(Call<HomeBean> call, Response<HomeBean> response) {
                List<HomeBean.DataBeanX.DataBean> data = response.body().getData().getData();
                Log.e("tag","aaaaa"+data.get(0).getActivityText());
                Log.e("tag","aaaaaaa"+data.toString());
                homeRecyclerAdapter.addList(data);
            }

            @Override
            public void onFailure(Call<HomeBean> call, Throwable t) {

            }
        });*/
    }

  /*  private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String homeHttp = RetrofitManager.getInstance().getHomeHttp();
                    if (homeHttp != null) {
                        final HomeBean homeBean = new Gson().fromJson(homeHttp, HomeBean.class);
                        final List<HomeBean.DataBeanX.DataBean> data = homeBean.getData().getData();
                        Log.e("tag", data.get(0).getActivityText());


                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                homeRecyclerAdapter.addList(data);
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }*/

    private void initView(View view) {
        RecyclerView recycler_home = view.findViewById(R.id.recycler_home);
        recycler_home.setLayoutManager(new LinearLayoutManager(getActivity()));
        homeRecyclerAdapter = new HomeRecyclerAdapter(getActivity());
        recycler_home.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        recycler_home.setAdapter(homeRecyclerAdapter);
    }

    @Override
    public void getSuccessData(List<HomeBean.DataBeanX.DataBean> object) {
        if(object!=null){

            Log.e("tag","aaaaa"+object.get(0).getActivityText());
            Log.e("tag","aaaaaaa"+object.toString());
            homeRecyclerAdapter.addList(object);

        }
    }

    @Override
    public void getError(String str) {

    }
}